

var InvestorModule = function () {
	
	$('#btn_reset').click(function(){
		$('#empGroupTree').jstree().refresh(true,true);
		$('#urls').val('');
	});
	
	var handleResourceTree = function() {
		$('#empGroupTree').jstree({
			'plugins' : [ "wholerow", "checkbox", "types" ],
			'core' : {
				"themes" : {
					"responsive" : false
				},
				'data' : {
					"url" : ctx + "/investor/empTree",
					"data" : function(node) {

					}
				}
			},
			"types" : {
				"default" : {
					"icon" : "fa fa-folder icon-warning icon-lg"
				},
				"file" : {
					"icon" : "fa fa-file icon-warning icon-lg"
				}
			}
		}).on("changed.jstree", function(e, data) {
			$('#urls').val(data.selected);
			var json = "{\"groupId\":\""+data.selected+"\"}"; 
			$.ajax({
            	contentType:"application/json; charset=UTF-8",
                dataType : 'json',
                type : "POST",
                url : ctx + "/appointment/queryEmpGroup",
                data : json,
                success: function(msg){
                	var data = msg.data;
                	if(data == null||data.length == 0){
                		$('#dept').val("");
                		return;
                	}
                	var str = "";
                	for(var i=0;i<data.length;i++){
                		str += data[i]+"  ";
            		}
                	$('#dept').val(str);
                },
                error: function (msg) {
                	$('#dept').val("");
                }
            });
		});
	};
    var editable = function () {
        return {
            init: function () {
                var oTable = $('#modelEditable').dataTable({
                    "lengthMenu": [
                        [10, 15, 20, 50],
                        [10, 15, 20, 50]
                    ],
                    "autoWidth":false,
                    "displayLength": 10,
                    "scrollX": true,
                    "language": {
                        "lengthMenu": "每页 _MENU_ 条记录",
                        "paginate": {
                            "previous": "上一页",
                            "next": "下一页"
                        },
                        "info" : "从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
                        "infoEmpty" : "记录数为0",
                    },
                    "columnDefs": [
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [0]
						},
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [1]
						},
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [2]
						}, 
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [3]
						}, 
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [4]
						}, 
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [5]
						}, 
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [6]
						}, 
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "-",
						    "targets": [7]
						}, 
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "-",
						    "targets": [8]
						}              
                    ],
                    "columns": [
                       { "data": "name" },
                       { "data": "productBriefName" },
                       { "data": "orderAmount" },
                       { "data": "groupName" },
                       { "data": "employeeName" },
                       { "data": "appointTime" },
                       { "data": "completeTime"},
                       { "data": "state" }
                   ],
                   "filter" : false,
                   "processing": false,
                   "serverSide": true,
                   "ajaxSource": ctx + "/appointment/query",
                   "serverMethod": "post",
                   "fnRowCallback" : function(nRow, aData, displayIndex) {
                	   nRow.children[0].id = aData.id;
                	   var gender = aData.gender;
                	   var investor
                	   if (aData.name == null) {
                		   aData.name = '-';
                		   investor = "<center>-</center>";
                	   }else{
                		   investor = "<center>" + aData.name + "<br>(" + aData.mobile + ")</center>";
                	   }
                	   $('td:eq(0)', nRow).html(investor);
                	   var contact
                	   if (aData.employeeNo == null) {
                		   aData.employeeNo = '-';
                		   contact = "<center>-</center>";
                	   }else{
                		   contact = "<center>" + aData.employeeName + "<br>(工号:" + aData.employeeNo + ")</center>";
                	   }
                	   $('td:eq(4)', nRow).html("<center>" + contact+"<center>");
                	   var state
                	   if(aData.state == 0){
                		   state = '未处理';
                	   }else if(aData.state == 1){
                		   state = '已处理';
                	   }else if(aData.state == 2){
                		   state = '已取消';
                	   }else if(aData.state == 3){
                		   state = '转订单';
                	   }else{
                		   state = '-';
                	   }
                	   $('td:eq(7)', nRow).html("<center>" + state+"<center>");
                	   $('td:eq(8)', nRow).html('<center><a class="detail fa fa-bars fa-lg " title="详情" href="javascript:;"></a><center>');
                       return nRow;
                   },
                   "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
	                   var json = "";
	                   var s;
	                   var groupId = $('#urls').val();
	                   for (var i = 0; i < dataSet.length; i++) {
	                       s = dataSet[i];
	                   	   if (s.name == 'sEcho') {
	                   		   json = json + '"echo":' + s.value;
		                   } else if (s.name == 'iDisplayStart') {
		                	   json = json + ',"page":' + s.value;
		                   } else if (s.name == 'iDisplayLength') {
		                	   json = json + ',"pageSize":' + s.value;
		                   }
	                   }
            	       var form = $("form#queryForm").serializeJSON();
                   	   var js = form.replace('}', ',');
                   	   json = js + json + "}";
                       $.ajax({
                       	   "contentType":"application/json; charset=UTF-8",
                           "dataType" : 'json',
                           "type" : "POST",
                           "url" : ctx + "/appointment/query",
                           "data" : json,
                           "success" : fnCallback
                       });
                   },
                });
                // 联系记录
                $('#modelEditable').on("click", "a.relation", (function (e) {
                    e.preventDefault();
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    window.location.href = ctx +"/appointment/relation/"+id;
                    return;
                }));
                // 明细
                $('#modelEditable').on("click", "a.detail", (function (e) {
                    e.preventDefault();
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    window.location.href = ctx +"/appointment/detail/"+id;
                    return;
                }));
                
                $('#btn_query').click(function(){
                	oTable.fnClearTable();
                });
            }

        };
    }();
    return {
        init: function () {
        	editable.init();
        	handleResourceTree();
        }
    };
}();
